---
title: "Code/Output Log"
subtitle: "Linguistic Assimilation Does Not Reduce Discrimination Against Immigrants"
author: "Donghyun Danny Choi, Mathias Poertner, Nicholas Sambanis"
date: "5/8/2020"
output: html_document
---

## Part I. Data Cleaning


```{r, message=F, warning=F}
# Load libraries
library(stargazer)
library(estimatr)
library(readr)
library(lubridate)
library(tidyverse)
library(Rmisc)
library(lfe)
library(kableExtra)
library(ebal)
```


```{r}
# Importing the raw experimental data for two experiments
datafirst  <- read.csv(file="data1.csv", stringsAsFactors = F)
datasecond <- read.csv(file="data2.csv", stringsAsFactors = F)

##### Creating Treatment Variables 

##  Experiment 1

## Main Figure 3: Discrimination Plot Comparisons
datafirst$language <- NA
# Hijab, German/ Foreign Language  (Column 1)
datafirst$language[datafirst$trtype==7 | datafirst$trtype==10] <- 1 
# No Hijab, German/ Foreign Language  (Column 2)
datafirst$language[datafirst$trtype==9 | datafirst$trtype==12]  <- 2 
# Native (Column 3)
datafirst$language[datafirst$trtype==14] <- 3 

# No hijab vs native comparison
datafirst$nhvn <- NA
datafirst$nhvn[datafirst$language==2] <- 1
datafirst$nhvn[datafirst$language==3] <- 0

# Hijab vs native comparison
datafirst$hvn <- NA
datafirst$hvn[datafirst$language==1] <- 1
datafirst$hvn[datafirst$language==3] <- 0

# Hijab vs no hijab comparison
datafirst$hvnh <- NA
datafirst$hvnh[datafirst$language==1] <- 1
datafirst$hvnh[datafirst$language==2] <- 0



## Main Figure 4: Language Effects Plot Comparisons
datafirst$languageoff <- NA
datafirst$languageoff[datafirst$trtype==7]   <- 1 # hijab foreign 
datafirst$languageoff[datafirst$trtype==10]  <- 2 # hijab German
datafirst$languageoff[datafirst$trtype==9]   <- 3 # no hijab foreign
datafirst$languageoff[datafirst$trtype==12]  <- 4 # no hijab German
datafirst$languageoff[datafirst$trtype==14]  <- 5 # native

# Hijab foreign Language vs Hijab German language
datafirst$hfvhg <- NA
datafirst$hfvhg[datafirst$trtype==7]  <- 1
datafirst$hfvhg[datafirst$trtype==10] <- 0

# No hijab foreign language vs No hijab German language
datafirst$nfvng <- NA
datafirst$nfvng[datafirst$trtype==9]  <- 1
datafirst$nfvng[datafirst$trtype==12] <- 0

# Hijab German language vs Native 
datafirst$hgvn <- NA
datafirst$hgvn[datafirst$trtype==10] <- 1 
datafirst$hgvn[datafirst$trtype==14]  <- 0 

# Hijab German language vs No hijab German language
datafirst$hgvnhg <- NA
datafirst$hgvnhg[datafirst$trtype==10] <- 1
datafirst$hgvnhg[datafirst$trtype==12] <- 0


## Experiment 2

## Main Figure 3: Discrimination Plot Comparisons

# Hijab, German/Foreign Language  (Column 1)
datasecond$language[datasecond$trtype==10 | datasecond$trtype==3] <- 1 
# No Hijab, German/Foreign Language  (Column 2)
datasecond$language[datasecond$trtype==11 | datasecond$trtype==6 ] <- 2 
# Native (Column 3)
datasecond$language[datasecond$trtype==9] <- 3 


# No hijab vs native comparison
datasecond$nhvn[datasecond$language==2] <- 1
datasecond$nhvn[datasecond$language==3] <- 0

# Hijab vs native comparison
datasecond$hvn[datasecond$language==1] <- 1
datasecond$hvn[datasecond$language==3] <- 0

# Hijab vs no hijab comparison
datasecond$hvnh <- NA
datasecond$hvnh[datasecond$language==1] <- 1
datasecond$hvnh[datasecond$language==2]  <- 0


## Main Figure 4: Language Effects Plot Comparisons
datasecond$languageoff[datasecond$trtype==10] <- 1 # hijab foreign
datasecond$languageoff[datasecond$trtype==3]  <- 2 # hijab german
datasecond$languageoff[datasecond$trtype==11] <- 3 # no hijab foreign
datasecond$languageoff[datasecond$trtype==6]  <- 4 # no hijab german                    
datasecond$languageoff[datasecond$trtype==9]  <- 5 # native german

# Hijab foreign language vs Hijab German language
datasecond$hfvhg <- NA
datasecond$hfvhg[datasecond$trtype==10] <- 1
datasecond$hfvhg[datasecond$trtype==3]  <- 0
  
# Hijab vs native comparison
datasecond$nfvng <- NA
datasecond$nfvng[datasecond$trtype==11] <- 1
datasecond$nfvng[datasecond$trtype==6]  <- 0

# Hijab German language vs Native 
datasecond$hgvn <- NA
datasecond$hgvn[datasecond$trtype==3] <- 1
datasecond$hgvn[datasecond$trtype==9] <- 0

# Hijab German language vs No hijab German language
datasecond$hgvnhg <- NA
datasecond$hgvnhg[datasecond$trtype==3] <- 1
datasecond$hgvnhg[datasecond$trtype==6] <- 0


##### Merging data from both experiments
colnames(datasecond)[colnames(datasecond)=="anyhelp"] <- "help"

# Coding which experiment
datafirst$exp  <- 1
datasecond$exp <- 2

# Merging the two experimental data sets
merged <- rbind(datafirst, datasecond)

# Drop observations that are not in any of the treatment conditions

merged <- merged %>%
  filter(!is.na(languageoff))


# Exporting merged data set to RData file.

save(merged, file = "merged.RData")



```


## Part II. Analysis for Main Paper (Figures and Tables)

```{r,  warning=F, message=F}
##### Main Figure 3: Dicrimination Plot

mergeddisttest <- Rmisc::summarySE(merged, measurevar="help", groupvars=c("language"))
mergeddisttest$point <- sprintf(mergeddisttest$help*100, fmt = '%#.2f')

# No hijab vs native comparison
summary(lm(help~nhvn, data=merged))

# Hijab vs native comparison
summary(lm(help~hvn, data=merged))

# Hijab vs no hijab comparison
summary(lm(help~hvnh, data=merged))



# Figure 3
interval1 <- -qnorm((1-0.90)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
mylabels <- c("(1)\nImmigrant Hijab\n(N=618)", 
              "(2)\nImmigrant No Hijab\n(N=638)", 
              "(3)\nNative\n(N=312)")

library(forcats)
pd <- position_dodge(0.1)
mergeddist <- ggplot(mergeddisttest, 
                     aes(x=factor(language), y=100*help, fill=factor(language))) +
  geom_bar(position=pd, stat="identity", width=0.45, alpha=0.9) +
  geom_errorbar(aes(ymin = 100*(help - se*interval2), 
                    ymax = 100*(help + se*interval2)),
                width = 0.1, position = position_dodge(width = 1/2)) +
  geom_text(aes(label = point), size = 4.0, 
            vjust=-0.5, hjust = -0.4, nudge_x = 0.05) +
  ggtitle(" ") +
  xlab("") +
  ylab("Assistance Rates (%)") +
  ylim(0.0, 110) +
  scale_x_discrete(breaks=c("1", "2", "3"), labels=mylabels)+
  theme_minimal() +
  theme(axis.text.y=element_text(colour = "gray30", size=11), 
        axis.ticks.y=element_blank()) +
  theme(axis.text.x=element_text(colour = "gray30", size=11), 
        axis.ticks.x=element_line(colour="gray30"))
mergeddist<- mergeddist + 
  geom_segment(aes(x=2, y=87, xend=2.2, yend=90), colour="darkgray") +
  geom_segment(aes(x=2.2, y=90, xend=2.8, yend=90), colour="darkgray") +
  geom_segment(aes(x=2.8, y=90, xend=3, yend=87), colour="darkgray")
mergeddist <- mergeddist + 
  annotate("text", x=2.5, y=93, label="-2.7%p (p=0.347)", size=3)
mergeddist<- mergeddist + 
  geom_segment(aes(x=1, y=83, xend=1.2, yend=86), colour="darkgray") +
  geom_segment(aes(x=1.2, y=86, xend=1.8, yend=86), colour="darkgray") +
  geom_segment(aes(x=1.8, y=86, xend=2, yend=83), colour="darkgray")
mergeddist <- mergeddist + 
  annotate("text", x=1.5, y=89, label="12.2%p (p<0.001)", size=3)
mergeddist<- mergeddist + 
  geom_segment(aes(x=1, y=98, xend=1.2, yend=101), colour="darkgray") +
  geom_segment(aes(x=1.2, y=101, xend=2.8, yend=101), colour="darkgray") +
  geom_segment(aes(x=2.8, y=101, xend=3, yend=98), colour="darkgray")
mergeddist <- mergeddist + 
  annotate("text", x=2, y=104, label="9.5%p (p=0.003)", size=3)
mergeddist <- mergeddist + 
  scale_fill_manual(values=c("#CC0000","#CC33CC", "#0000FF"))
mergeddist <- mergeddist + theme(legend.position="none")
print(mergeddist)



##### Main Figure 4: Language Effects
mergedofftest <- Rmisc::summarySE(merged, measurevar="help", groupvars=c("languageoff"))
mergedofftest$point <- sprintf(mergedofftest$help*100, fmt = '%#.2f')

# Hijab foreign language vs Hijab German language
summary(lm(help~hfvhg, data=merged))

# No hijab foreign language vs No hijab German language
summary(lm(help~nfvng, data=merged))

# Hijab German language vs Native 
summary(lm(help~hgvn, data=merged))

# Hijab German language vs No hijab German language
summary(lm(help~hgvnhg, data=merged))

# Figure 4
interval1 <- -qnorm((1-0.90)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
mylabels <- c("(1)\nHijab\nForeign Language\n(N=284)", 
              "(2)\nHijab\nGerman Language\n(N=334)", 
              "(3)\nNo Hijab\nForeign Language\n(N=309)",
              "(4)\nNo Hijab\nGerman Language\n(N=329)", 
              "(5)\nNative\nGerman Language\n(N=312)")


pd <- position_dodge(0.1)
mergedoffset <- ggplot(mergedofftest, 
                       aes(x=factor(languageoff), y=100*help, fill=factor(languageoff))) +
  geom_bar(position=pd, stat="identity", width=0.45, alpha=0.9) +
  geom_errorbar(aes(ymin = 100*(help - se*interval2), ymax = 100*(help + se*interval2)),
                width = 0.1, position = position_dodge(width = 1/2)) +
  geom_text(aes(label = point), 
            size = 4.0, vjust=-0.5, hjust = -0.4, nudge_x = 0.05) +
  ggtitle(" ") +
  xlab("") +
  ylab("Assistance Rates (%)") +
  ylim(0.0, 110) +
  scale_x_discrete(breaks=c("1", "2", "3", "4", "5"), labels=mylabels)+
  theme_minimal() +
  theme(axis.text.y=element_text(colour = "gray30", size=9), 
        axis.ticks.y=element_blank()) +
  theme(axis.text.x=element_text(colour = "gray30", size=9),
        axis.ticks.x=element_line(colour="gray30"))
mergedoffset<- mergedoffset + 
  geom_segment(aes(x=1, y=77, xend=1.2, yend=80), colour="darkgray") +
  geom_segment(aes(x=1.2, y=80, xend=1.8, yend=80), colour="darkgray") +
  geom_segment(aes(x=1.8, y=80, xend=2, yend=77), colour="darkgray")
mergedoffset <- mergedoffset + 
  annotate("text", x=1.5, y=83, label="0.07%p (p=0.984)", size=3)
mergedoffset<- mergedoffset + 
  geom_segment(aes(x=3, y=87, xend=3.2, yend=90), colour="darkgray") +
  geom_segment(aes(x=3.2, y=90, xend=3.8, yend=90), colour="darkgray") +
  geom_segment(aes(x=3.8, y=90, xend=4, yend=87), colour="darkgray")
mergedoffset <- mergedoffset + 
  annotate("text", x=3.5, y=93, label="3.3%p (p=0.320)", size=3)
mergedoffset<- mergedoffset + 
  geom_segment(aes(x=2, y=95, xend=2.2, yend=98), colour="darkgray") +
  geom_segment(aes(x=2.2, y=98, xend=3.8, yend=98), colour="darkgray") +
  geom_segment(aes(x=3.8, y=98, xend=4, yend=95), colour="darkgray")
mergedoffset <- mergedoffset + 
  annotate("text", x=3, y=101, label="13.7%p (p<0.0001)", size=3)
mergedoffset<- mergedoffset + 
  geom_segment(aes(x=2, y=103, xend=2.2, yend=106), colour="darkgray") +
  geom_segment(aes(x=2.2, y=106, xend=4.8, yend=106), colour="darkgray") +
  geom_segment(aes(x=4.8, y=106, xend=5, yend=103), colour="darkgray")
mergedoffset <- mergedoffset + 
  annotate("text", x=3.5, y=109, label="9.4%p (p=0.009)", size=3)
mergedoffset <- mergedoffset + 
  scale_fill_manual(values=c("#CC0000","#FF0000","#CC33CC","#9900FF", "#0000FF"))
mergedoffset <- mergedoffset + theme(legend.position="none")
print(mergedoffset)



##### Table 2: ATEs for Linguistic Differences

# Hypothesis 1: Bystanders help immigrants 
# speaking a foreign language less than natives

merged$hijab <- NA
merged$hijab[merged$languageoff==1 | merged$languageoff==2] <- 1
merged$hijab[merged$languageoff==3 | merged$languageoff==4 | merged$languageoff==5] <-0

merged$foreign <- NA
merged$foreign[merged$languageoff==1 | merged$languageoff==3] <- 1
merged$foreign[merged$languageoff==2 | merged$languageoff==4 | merged$languageoff==5] <- 0


ifvn   <- felm(help ~ hijab*foreign | 0 | 0 | 0, data=merged)
ifvnFE <- felm(help ~ hijab*foreign | exp + bystander | 0 | 0, data=merged)
ifvnFEt <- felm(help ~ hijab*foreign | exp + bystander + team | 0 | 0, data=merged)


# Hypothesis 2: Bystanders are less likely to help immigrants 
# speaking a foreign language than immigrants speaking German

merged$ifvig <- NA
merged$ifvig[merged$languageoff==1 | merged$languageoff==3] <- 1
merged$ifvig[merged$languageoff==2 | merged$languageoff==4] <- 0

ifvig   <-  felm(help ~ ifvig | 0 | 0 | 0, data=merged)
ifvigFE <-  felm(help ~ ifvig | exp + bystander | 0 | 0, data=merged)
ifvigFEt <- felm(help ~ ifvig | exp + bystander + team | 0 | 0, data=merged)
```

```{r, warning=F, message=F, results="asis"}
stargazer(ifvn, ifvnFE, ifvnFEt, ifvig, ifvigFE, ifvigFEt,
          dep.var.labels  = c("Any help?"),
          type="html", 
          column.separate = c(3,3),
          omit.stat = c("f"),
          keep=c("hijab", "foreign", "hijab:foreign", "ifvig",  "Constant"),
          keep.stat=c("n", "rsq"),
          add.lines = list(c("Sample", "Full", "Full", "Full", 
                             "Immigrant", "Immigrant", "Immigrant"),
                           c("Experiment FE", "No", "Yes", "Yes", "No", "Yes", "Yes"),
                           c("Bystander FE",  "No", "Yes", "Yes", "No", "Yes", "Yes"),
                           c( "Team FE", "No", "No", "Yes", "No", "No", "Yes")))

```


## Part III. SI Appendix

```{r, warning=F, message=F}
datafirst  <- merged[which(merged$exp==1), ]
datasecond <- merged[which(merged$exp==2), ]


##### II. Logistics and Procedures

# Figure S2: Study sites

# Maps for the sites 

library(ggmap)
library(maptools)
library(sp)
library(rgdal)
library(tmap)
library(rgeos)
library(plyr)
library(ggrepel)

stat_west <- read.csv(file="map/StationList_west.csv")
stat_east <- read.csv(file="map/StationList_east.csv")

ogrListLayers("map/DEU_adm1.shp") #will show you available layers for the above dataset
germany<- readOGR("map/DEU_adm1.shp", layer="DEU_adm1")
germany@data$id <- rownames(germany@data)
germany.points = fortify(germany, region="id")
germany.df = join(germany.points, germany@data, by="id")


germany.df$states <- 0
germany.df$states[germany.df$NAME_1=="Nordrhein-Westfalen"] <- 1
germany.df$states[germany.df$NAME_1=="Sachsen"] <- 1
germany.df$states[germany.df$NAME_1=="Brandenburg"] <- 1
germany.df$states[germany.df$NAME_1=="Niedersachsen"] <- 1

map <- ggplot() +
  geom_polygon(data = germany.df,
               aes(x = long, y = lat, group = group),
               color = 'grey', fill = 'white', size = 0.2, alpha=0.8) +
  geom_polygon(data = germany.df, aes(x = long, y = lat, 
                                      fill=factor(states), alpha=0.2, group = group))+
  theme_void()

map <- map + geom_point(aes(x=longitude, y=latitude), 
                        data=stat_west, size=1, color="darkblue", alpha=0.5)
map <- map + geom_point(aes(x=longitude, y=latitude), 
                        data=stat_east, size=1, color="darkblue", alpha=0.5)


map <- map + geom_point(aes(x=longitude, y=latitude), 
                        data=stat_west, size=2.5, color="darkblue", alpha=0.5)
map <- map + geom_point(aes(x=longitude, y=latitude), 
                        data=stat_east, size=2.5, color="darkblue", alpha=0.5)

map <- map + theme(legend.position="none")
map <- map + theme(axis.text.y=element_blank(), axis.ticks.y=element_blank(),
                   axis.text.x=element_blank(), axis.ticks.x=element_blank())
map <- map + xlab(" ")
map <- map + ylab(" ")

map <- map + theme(axis.title.x=element_blank(),
                   axis.text.x=element_blank(),
                   axis.ticks.x=element_blank(),
                   axis.title.y=element_blank(),
                   axis.text.y=element_blank(),
                   axis.ticks.y=element_blank(),
                   panel.grid.major = element_blank(), 
                   panel.grid.minor = element_blank(),
                   panel.background = element_blank())

print(map)

ggsave("map/MapLanguage.png", width=4.1, height=5.3)


##### III. Descriptive Statistics
set.seed(1004)
library(Matching)

# Declaring covariates
covariates <- c("bystander", "hpprop", "femprop", "temp", "rush")
teststat <- c("mean.Tr", "mean.Co", "T pval")
sumstat <- merged[covariates]
```

```{r, warning=F, message=F, results="asis"}
# Descriptive Statistics
stargazer(sumstat, type="html")
```

```{r, warning=F, message=F, echo=T, results="hide"}
##### IV. Covariate Balance

## Figure 2 balance tests 

library(Matching)
library(ebal)

# Hijab vs native
mb_hvn <- MatchBalance(hvn ~ bystander + hpprop + femprop + 
                       temp + rush, data=merged, match.out=NULL, nboots=5000)
mb_hvn <- ebal::baltest.collect(mb_hvn, 
                                var.names=covariates, after = FALSE) # compiling balance statistics 
mb_hvn <- as.data.frame(mb_hvn)
mb_hvn <- mb_hvn[teststat] # coercing into dataframe

```

```{r, warning=F, message=F, results="asis"}
kable(mb_hvn, "html", booktabs=TRUE) %>%
  kable_styling(position="center")
```

```{r, warning=F, message=F, echo=T, results="hide"}
# Hijab vs no hijab
mb_hvnh <- MatchBalance(hvnh ~ bystander+hpprop+femprop+
                          temp+rush, data=merged, match.out=NULL, nboots=5000)
mb_hvnh <- ebal::baltest.collect(mb_hvnh, var.names=covariates, after = FALSE) # compiling balance statistics 
mb_hvnh <- as.data.frame(mb_hvnh)
mb_hvnh <- mb_hvnh[teststat] # coercing into dataframe
```

```{r, warning=F, message=F, results="asis"}
kable(mb_hvnh, "html", booktabs=TRUE) %>%
  kable_styling(position="center")
```

```{r, warning=F, message=F, echo=T, results="hide"}
# No hijab vs native
mb_nhvn <- MatchBalance(nhvn ~ bystander+hpprop+femprop+
                          temp+rush, data=merged, match.out=NULL, nboots=5000)
mb_nhvn <- ebal::baltest.collect(mb_nhvn, var.names=covariates, after = FALSE) # compiling balance statistics 
mb_nhvn <- as.data.frame(mb_nhvn)
mb_nhvn <- mb_nhvn[teststat] # coercing into dataframe
```

```{r, warning=F, message=F, results="asis"}
kable(mb_nhvn, "html", booktabs=TRUE) %>%
  kable_styling(position="center")
```


```{r, warning=F, message=F, echo=T, results="hide"}
## Figure 3 balance tests 

# Hijab foreign language vs hijab German language
mb_hfvhg <- MatchBalance(hfvhg ~ bystander+hpprop+femprop+
                           temp+rush, data=merged, match.out=NULL, nboots=5000)
mb_hfvhg <- ebal::baltest.collect(mb_hfvhg, var.names=covariates, after = FALSE) # compiling balance statistics 
mb_hfvhg <- as.data.frame(mb_hfvhg)
mb_hfvhg <- mb_hfvhg[teststat] # coercing into dataframe
```

```{r, warning=F, message=F, results="asis"}
kable(mb_hfvhg, "html", booktabs=TRUE) %>%
  kable_styling(position="center")
```

```{r, warning=F, message=F, echo=T, results="hide"}
# No hijab foreign language vs no hijab German language
mb_nfvng <- MatchBalance(nfvng ~ bystander+hpprop+femprop+
                           temp+rush, data=merged, match.out=NULL, nboots=5000)
mb_nfvng <- ebal::baltest.collect(mb_nfvng, var.names=covariates, after = FALSE) # compiling balance statistics 
mb_nfvng <- as.data.frame(mb_nfvng)
mb_nfvng <- mb_nfvng[teststat] # coercing into dataframe
```

```{r, warning=F, message=F, results="asis"}
kable(mb_nfvng, "html", booktabs=TRUE) %>%
  kable_styling(position="center")
```

```{r, warning=F, message=F, echo=T, results="hide"}
# Hijab German language vs native
mb_hgvn <- MatchBalance(hgvn ~ bystander+hpprop+femprop+
                          temp+rush, data=merged, match.out=NULL, nboots=5000)
mb_hgvn <- ebal::baltest.collect(mb_hgvn, var.names=covariates, after = FALSE) # compiling balance statistics 
mb_hgvn <- as.data.frame(mb_hgvn)
mb_hgvn <- mb_hgvn[teststat] # coercing into dataframe
```

```{r, warning=F, message=F, results="asis"}
kable(mb_hgvn, "html", booktabs=TRUE) %>%
  kable_styling(position="center")
```

```{r, warnings=F, message=F}
##### V. Additional Analyses

# Figure S3: Offsetting effects - Experiment 1
langofftest1 <- Rmisc::summarySE(datafirst, measurevar="help", groupvars=c("languageoff"))
langofftest1$point <- sprintf(langofftest1$help*100, fmt = '%#.2f')

interval1 <- -qnorm((1-0.90)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
mylabels <- c("(1)\nHijab\nForeign Language\n(N=111)", 
              "(2)\nHijab\nGerman Language\n(N=119)", 
              "(3)\nNo Hijab\nForeign Language\n(N=116)",
              "(4)\nNo Hijab\nGerman Language\n(N=122)", 
              "(5)\nNative\nGerman Language\n(N=120)")


pd <- position_dodge(0.1)
langoffset1 <- ggplot(langofftest1, aes(x=factor(languageoff), 
                                        y=100*help, fill=factor(languageoff))) +
  geom_bar(position=pd, stat="identity", width=0.45, alpha=0.9) +
  geom_errorbar(aes(ymin = 100*(help - se*interval2), ymax = 100*(help + se*interval2)),
                width = 0.1, position = position_dodge(width = 1/2)) +
  geom_text(aes(label = point), size = 4.0, vjust=-0.5, hjust = -0.4, nudge_x = 0.05) +
  ggtitle(" ") +
  xlab("") +
  ylab("Assistance Rates (%)") +
  ylim(0.0, 110) +
  scale_x_discrete(breaks=c("1", "2", "3", "4", "5"), labels=mylabels)+
  theme_minimal() +
  theme(axis.text.y=element_text(colour = "gray30", size=9), 
        axis.ticks.y=element_blank()) +
  theme(axis.text.x=element_text(colour = "gray30", size=9), 
        axis.ticks.x=element_line(colour="gray30"))
langoffset1<- langoffset1 + 
  geom_segment(aes(x=1, y=87, xend=1.2, yend=90), colour="darkgray") +
  geom_segment(aes(x=1.2, y=90, xend=1.8, yend=90), colour="darkgray") +
  geom_segment(aes(x=1.8, y=90, xend=2, yend=87), colour="darkgray")
langoffset1 <- langoffset1 + 
  annotate("text", x=1.5, y=93, label="5.3%p (p=0.408)", size=3)
langoffset1<- langoffset1 + 
  geom_segment(aes(x=3, y=93, xend=3.2, yend=96), colour="darkgray") +
  geom_segment(aes(x=3.2, y=96, xend=3.8, yend=96), colour="darkgray") +
  geom_segment(aes(x=3.8, y=96, xend=4, yend=93), colour="darkgray")
langoffset1 <- langoffset1 + annotate("text", x=3.5, y=99, label="1.1%p (p=0.834)", size=3)
langoffset1<- langoffset1 + 
  geom_segment(aes(x=2, y=103, xend=2.2, yend=106), colour="darkgray") +
  geom_segment(aes(x=2.2, y=106, xend=4.8, yend=106), colour="darkgray") +
  geom_segment(aes(x=4.8, y=106, xend=5, yend=103), colour="darkgray")
langoffset1 <- langoffset1 + 
  annotate("text", x=3.5, y=109, label="10.3%p (p=0.0878)", size=3)
langoffset1 <- langoffset1 + 
  scale_fill_manual(values=c("#CC0000","#FF0000","#CC33CC","#9900FF", "#0000FF"))
langoffset1 <- langoffset1 + theme(legend.position="none")
print(langoffset1)


# Figure S4: Offsetting effects - Experiment 2 
langofftest <- Rmisc::summarySE(datasecond, measurevar="help", groupvars=c("languageoff"))
langofftest$point <- sprintf(langofftest$help*100, fmt = '%#.2f')

interval1 <- -qnorm((1-0.90)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
mylabels <- c("(1)\nHijab\nForeign Language\n(N=173)", 
              "(2)\nHijab\nGerman Language\n(N=215)",
              "(3)\nNo Hijab\nForeign Language\n(N=193)",
              "(4)\nNo Hijab\nGerman Language\n(N=207)", 
              "(5)\nNative\nGerman Language\n(N=192)")

pd <- position_dodge(0.1)
langoffset2 <- ggplot(langofftest, aes(x=factor(languageoff), y=100*help, fill=factor(languageoff))) +
  geom_bar(position=pd, stat="identity", width=0.45, alpha=0.9) +
  geom_errorbar(aes(ymin = 100*(help - se*interval2), ymax = 100*(help + se*interval2)),
                width = 0.1, position = position_dodge(width = 1/2)) +
  geom_text(aes(label = point), size = 4.0, vjust=-0.5, hjust = -0.4, nudge_x = 0.05) +
  ggtitle(" ") +
  xlab("") +
  ylab("Assistance Rates (%)") +
  ylim(0.0, 110) +
  scale_x_discrete(breaks=c("1", "2", "3", "4", "5"), labels=mylabels)+
  theme_minimal() +
  theme(axis.text.y=element_text(colour = "gray30", size=9), 
        axis.ticks.y=element_blank()) +
  theme(axis.text.x=element_text(colour = "gray30", size=9), 
        axis.ticks.x=element_line(colour="gray30"))
langoffset2<- langoffset2 + 
  geom_segment(aes(x=1, y=87, xend=1.2, yend=90), colour="darkgray") +
  geom_segment(aes(x=1.2, y=90, xend=1.8, yend=90), colour="darkgray") +
  geom_segment(aes(x=1.8, y=90, xend=2, yend=87), colour="darkgray")
langoffset2 <- langoffset2 + 
  annotate("text", x=1.5, y=93, label="-3.5%p (p=0.456)", size=3)
langoffset2<- langoffset2 + 
  geom_segment(aes(x=3, y=93, xend=3.2, yend=96), colour="darkgray") +
  geom_segment(aes(x=3.2, y=96, xend=3.8, yend=96), colour="darkgray") +
  geom_segment(aes(x=3.8, y=96, xend=4, yend=93), colour="darkgray")
langoffset2 <- langoffset2 + annotate("text", x=3.5, y=99, label="4.5%p (p=0.274)", size=3)
langoffset2<- langoffset2 + 
  geom_segment(aes(x=2, y=103, xend=2.2, yend=106), colour="darkgray") +
  geom_segment(aes(x=2.2, y=106, xend=4.8, yend=106), colour="darkgray") +
  geom_segment(aes(x=4.8, y=106, xend=5, yend=103), colour="darkgray")
langoffset2 <- langoffset2 + 
  annotate("text", x=3.5, y=109, label="9.1%p (p=0.0438)", size=3)
langoffset2 <- langoffset2 + 
  scale_fill_manual(values=c("#CC0000","#FF0000","#CC33CC","#9900FF", "#0000FF"))
langoffset2 <- langoffset2 + theme(legend.position="none")
print(langoffset2)

# Figure S5/S6: Discrimination plot for West and East Germany

# Subsetting Data to West/East Germany
table(merged$team)
merged$region <- NA

# region==1 if West germany, region==2 if East Germany
merged$region <- ifelse(merged$team=="NRW1" | merged$team=="NRW2"  | merged$team=="NRW3" |
                        merged$team=="1" | merged$team=="2" | merged$team=="3" | merged$team=="4" , 1, 2)
table(merged$region)

# Subsetting data
west <- merged[which(merged$region==1), ]
east <- merged[which(merged$region==2), ]


# Figure S5: West Germany
mergedofftestw <- Rmisc::summarySE(west, measurevar="help", groupvars=c("languageoff"))
mergedofftestw$point <- sprintf(mergedofftestw$help*100, fmt = '%#.2f')


interval1 <- -qnorm((1-0.90)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
mylabels <- c("(1)\nHijab\nForeign Language\n(N=159)", 
              "(2)\nHijab\nGerman Language\n(N=213)", 
              "(3)\nNo Hijab\nForeign Language\n(N=178)",
              "(4)\nNo Hijab\nGerman Language\n(N=206)", 
              "(5)\nNative\nGerman Language\n(N=200)")


pd <- position_dodge(0.1)
mergedoffsetw <- ggplot(mergedofftestw, 
                        aes(x=factor(languageoff), y=100*help, fill=factor(languageoff))) +
  geom_bar(position=pd, stat="identity", width=0.45, alpha=0.9) +
  geom_errorbar(aes(ymin = 100*(help - se*interval2), ymax = 100*(help + se*interval2)),
                width = 0.1, position = position_dodge(width = 1/2)) +
  geom_text(aes(label = point), size = 4.0, vjust=-0.5, hjust = -0.4, nudge_x = 0.05) +
  ggtitle(" ") +
  xlab("") +
  ylab("Assistance Rates (%)") +
  ylim(0.0, 110) +
  scale_x_discrete(breaks=c("1", "2", "3", "4", "5"), labels=mylabels)+
  theme_minimal() +
  theme(axis.text.y=element_text(colour = "gray30", size=9), 
        axis.ticks.y=element_blank()) +
  theme(axis.text.x=element_text(colour = "gray30", size=9), 
        axis.ticks.x=element_line(colour="gray30"))
mergedoffsetw<- mergedoffsetw + 
  geom_segment(aes(x=1, y=80, xend=1.2, yend=83), colour="darkgray") +
  geom_segment(aes(x=1.2, y=83, xend=1.8, yend=83), colour="darkgray") +
  geom_segment(aes(x=1.8, y=83, xend=2, yend=80), colour="darkgray")
mergedoffsetw <- mergedoffsetw + 
  annotate("text", x=1.5, y=86, label="-0.06%p (p=0.893)", size=3)
mergedoffsetw<- mergedoffsetw + 
  geom_segment(aes(x=3, y=87, xend=3.2, yend=90), colour="darkgray") +
  geom_segment(aes(x=3.2, y=90, xend=3.8, yend=90), colour="darkgray") +
  geom_segment(aes(x=3.8, y=90, xend=4, yend=87), colour="darkgray")
mergedoffsetw <- mergedoffsetw + 
  annotate("text", x=3.5, y=93, label="2.6%p (p=0.540)", size=3)
mergedoffsetw<- mergedoffsetw + 
  geom_segment(aes(x=2, y=95, xend=2.2, yend=98), colour="darkgray") +
  geom_segment(aes(x=2.2, y=98, xend=3.8, yend=98), colour="darkgray") +
  geom_segment(aes(x=3.8, y=98, xend=4, yend=95), colour="darkgray")
mergedoffsetw <- mergedoffsetw + 
  annotate("text", x=3, y=101, label="9.7%p (p=0.022)", size=3)
mergedoffsetw<- mergedoffsetw + 
  geom_segment(aes(x=2, y=103, xend=2.2, yend=106), colour="darkgray") +
  geom_segment(aes(x=2.2, y=106, xend=4.8, yend=106), colour="darkgray") +
  geom_segment(aes(x=4.8, y=106, xend=5, yend=103), colour="darkgray")
mergedoffsetw <- mergedoffsetw + annotate("text", x=3.5, y=109, label="3.1%p (p=0.488)", size=3)
mergedoffsetw <- mergedoffsetw + 
  scale_fill_manual(values=c("#CC0000","#FF0000","#CC33CC","#9900FF", "#0000FF"))
mergedoffsetw <- mergedoffsetw + theme(legend.position="none")
print(mergedoffsetw)


# Figure S6: East Germany
mergedoffteste <- Rmisc::summarySE(east, measurevar="help", groupvars=c("languageoff"))
mergedoffteste$point <- sprintf(mergedoffteste$help*100, fmt = '%#.2f')


interval1 <- -qnorm((1-0.90)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
mylabels <- c("(1)\nHijab\nForeign Language\n(N=125)", 
              "(2)\nHijab\nGerman Language\n(N=121)", 
              "(3)\nNo Hijab\nForeign Language\n(N=131)",
              "(4)\nNo Hijab\nGerman Language\n(N=123)", 
              "(5)\nNative\nGerman Language\n(N=112)")


pd <- position_dodge(0.1)
mergedoffsete <- ggplot(mergedoffteste, 
                        aes(x=factor(languageoff), y=100*help, fill=factor(languageoff))) +
  geom_bar(position=pd, stat="identity", width=0.45, alpha=0.9) +
  geom_errorbar(aes(ymin = 100*(help - se*interval2), ymax = 100*(help + se*interval2)),
                width = 0.1, position = position_dodge(width = 1/2)) +
  geom_text(aes(label = point), size = 4.0, vjust=-0.5, hjust = -0.4, nudge_x = 0.05) +
  ggtitle(" ") +
  xlab("") +
  ylab("Assistance Rates (%)") +
  ylim(0.0, 110) +
  scale_x_discrete(breaks=c("1", "2", "3", "4", "5"), labels=mylabels)+
  theme_minimal() +
  theme(axis.text.y=element_text(colour = "gray30", size=9), 
        axis.ticks.y=element_blank()) +
  theme(axis.text.x=element_text(colour = "gray30", size=9), 
        axis.ticks.x=element_line(colour="gray30"))
mergedoffsete<- mergedoffsete + 
  geom_segment(aes(x=1, y=80, xend=1.2, yend=83), colour="darkgray") +
  geom_segment(aes(x=1.2, y=83, xend=1.8, yend=83), colour="darkgray") +
  geom_segment(aes(x=1.8, y=83, xend=2, yend=80), colour="darkgray")
mergedoffsete <- mergedoffsete + 
  annotate("text", x=1.5, y=86, label="-1.4%p (p=0.828)", size=3)
mergedoffsete<- mergedoffsete + 
  geom_segment(aes(x=3, y=87, xend=3.2, yend=90), colour="darkgray") +
  geom_segment(aes(x=3.2, y=90, xend=3.8, yend=90), colour="darkgray") +
  geom_segment(aes(x=3.8, y=90, xend=4, yend=87), colour="darkgray")
mergedoffsete <- mergedoffsete + 
  annotate("text", x=3.5, y=93, label="4.0%p (p=0.457)", size=3)
mergedoffsete<- mergedoffsete + 
  geom_segment(aes(x=2, y=95, xend=2.2, yend=98), colour="darkgray") +
  geom_segment(aes(x=2.2, y=98, xend=3.8, yend=98), colour="darkgray") +
  geom_segment(aes(x=3.8, y=98, xend=4, yend=95), colour="darkgray")
mergedoffsete <- mergedoffsete + 
  annotate("text", x=3, y=101, label="21.0%p (p=0.0004)", size=3)
mergedoffsete<- mergedoffsete + 
  geom_segment(aes(x=2, y=103, xend=2.2, yend=106), colour="darkgray") +
  geom_segment(aes(x=2.2, y=106, xend=4.8, yend=106), colour="darkgray") +
  geom_segment(aes(x=4.8, y=106, xend=5, yend=103), colour="darkgray")
mergedoffsete <- mergedoffsete + 
  annotate("text", x=3.5, y=109, label="20.7%p (p=0.0008)", size=3)
mergedoffsete <- mergedoffsete + 
  scale_fill_manual(values=c("#CC0000","#FF0000","#CC33CC","#9900FF", "#0000FF"))
mergedoffsete <- mergedoffsete + theme(legend.position="none")
print(mergedoffsete)

```

```{r, warning=F, message=F, results="asis"}
##### VI. Equivalence Tests 

library(TOSTER) # For implementing two one-sided tests of proportions

# Create treatment variable for test 1: immigrant foreign (hijab, no hijab pooled) vs immigrant German

merged$ifvig <- NA
merged$ifvig[merged$languageoff==1 | merged$languageoff==3] <- 1
merged$ifvig[merged$languageoff==2 | merged$languageoff==4] <- 0

langtesteq  <- Rmisc::summarySE(merged, measurevar="help", groupvars=c("ifvig"))
langtesteq2 <- Rmisc::summarySE(merged, measurevar="help", groupvars=c("hfvhg"))


TOSTtwo.prop(0.7239, 0.7099,  n1=663, n2=593, 
             low_eqbound=-0.04, high_eqbound=0.04, alpha = 0.05)
TOSTtwo.prop(0.6556, 0.65499, n1=334, n2=284, 
             low_eqbound=-0.04, high_eqbound=0.04, alpha = 0.05)

TOSTtwo.prop(0.7239, 0.7099,  n1=663, n2=593, 
             low_eqbound=-0.06, high_eqbound=0.06, alpha = 0.05)
TOSTtwo.prop(0.6556, 0.65499, n1=334, n2=284, 
             low_eqbound=-0.06, high_eqbound=0.06, alpha = 0.05)

TOSTtwo.prop(0.7239, 0.7099,  n1=663, n2=593, 
             low_eqbound=-0.08, high_eqbound=0.08, alpha = 0.05)
TOSTtwo.prop(0.6556, 0.65499, n1=334, n2=284, 
             low_eqbound=-0.08, high_eqbound=0.08, alpha = 0.05)



##### VII. Effects by Foreign Language Used

# Coding Turkish vs Arabic for foreign language used
merged$turkish <- 0
merged$turkish[merged$team=="Bburg1" | merged$team=="NRW3" | merged$team=="Sachsen1" |
               merged$team=="1" | merged$team=="2" | merged$team=="4" | merged$team=="6"] <- 1


# Table S5: Effects by foreign language used

turkish  <- lm(help~hfvhg, data=subset(merged, turkish==1))
arabic   <- lm(help~hfvhg, data=subset(merged, turkish==0))

bothlang <- lm(help~hfvhg*turkish, data=merged)



stargazer(turkish, arabic, bothlang, 
          dep.var.labels  = c("Any help?"),
          type="html", 
          omit.stat = c("f"),
          keep=c("hfvhg", "turkish", "hfvhg:turkish", "Constant"),
          keep.stat=c("n", "rsq"),
          add.lines = list(c("Foreign Language", "Turkish", "Arabic", "Merged")))

```

```{r, warning=F, message=F}
##### VIII. Manipulation Checks on Perception of Confederate Ethnicity

# Import manipulation check data set 
mcheck <- read.csv(file="mcheck.csv")


mcheck$q1 <- ifelse(mcheck$Q1==1, 1, 0)
mcheck$q2 <- ifelse(mcheck$Q2==1, 1, 0)
mcheck$q3 <- ifelse(mcheck$Q3==1, 1, 0)
mcheck$q4 <- ifelse(mcheck$Q4==1, 1, 0)
mcheck$q5 <- ifelse(mcheck$Q5==1, 1, 0)
mcheck$q6 <- ifelse(mcheck$Q6==1, 1, 0)
mcheck$q7 <- ifelse(mcheck$Q7==1, 1, 0)
mcheck$q8 <- ifelse(mcheck$Q8==1, 1, 0)
mcheck$q9 <- ifelse(mcheck$Q9==1, 1, 0)
mcheck$q10 <- ifelse(mcheck$Q10==1, 1, 0)
mcheck$q11 <- ifelse(mcheck$Q11==1, 1, 0)
mcheck$q12 <- ifelse(mcheck$Q12==1, 1, 0)
mcheck$q13 <- ifelse(mcheck$Q13==1, 1, 0)
mcheck$q14 <- ifelse(mcheck$Q14==1, 1, 0)
mcheck$q15 <- ifelse(mcheck$Q15==1, 1, 0)

# Classifying photos of native vs immigrant confederates
native <- c("q1","q2", "q5", "q6", "q9", "q12", "q13", "q14")
immigrant <- c("q3", "q4", "q7", "q8", "q10", "q15")

checknative <- mcheck[native] 
checknative <- checknative[complete.cases(checknative), ]

checkimm <- mcheck[immigrant]
checkimm <- checkimm[complete.cases(checknative), ]

library(tidyr)

# Collapsing all native vs immigrant 
checkn_long <- checknative %>%
  gather(item, percent, q1:q14)
checkn_long$category <- "native"

mean(checkn_long$percent, na.rm=T)

checkimm <- mcheck[immigrant]
checki_long <- checkimm %>%
  gather(item, percent, q3:q15)
checki_long$category <- "immigrant"

mean(checki_long$percent, na.rm=T)

# Creating merged data frame
manipcheck1 <- rbind(checkn_long, checki_long)

# Implement t-test
t.test(manipcheck1$percent ~ manipcheck1$category)

```

